/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.wms.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;

import com.google.gson.Gson;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.wms.entity.StorageInform;
import com.jeeplus.modules.wms.service.StorageInformService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.config.Global;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ExportExcel;
import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.modules.wms.entity.ReceivingNotes;
import com.jeeplus.modules.wms.service.ReceivingNotesService;

/**
 * 收货单Controller
 * @author sld
 * @version 2017-12-12
 */
@Controller
@RequestMapping(value = "${adminPath}/wms/receivingNotes")
public class ReceivingNotesController extends BaseController {

	@Autowired
	private ReceivingNotesService receivingNotesService;
	@Autowired
	private StorageInformService storageInformService;
	
	@ModelAttribute
	public ReceivingNotes get(@RequestParam(required=false) String id) {
		ReceivingNotes entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = receivingNotesService.get(id);
		}
		if (entity == null){
			entity = new ReceivingNotes();
		}
		return entity;
	}
	
	/**
	 * 收货单列表页面
	 */
	@RequiresPermissions("wms:receivingNotes:list")
	@RequestMapping(value = {"list", ""})
	public String list() {
		return "modules/wms/receivingNotesList";
	}
	
		/**
	 * 收货单列表数据
	 */
	@ResponseBody
	@RequiresPermissions("wms:receivingNotes:list")
	@RequestMapping(value = "data")
	public Map<String, Object> data(ReceivingNotes receivingNotes, HttpServletRequest request, HttpServletResponse response, Model model) {
		String pro=UserUtils.getUseroffice();
		if(!pro.equals("总部")){receivingNotes.setProjectName(pro);}
		if(receivingNotes.getMaterialName()!=null &receivingNotes.getMaterialName()!=""){
			String name=receivingNotes.getMaterialName();
			String a="";
			String b="";
			String c="";
			String d="";
			String e="";
			String f="";
			String g="";
			String n="";
			boolean bol=true;
			if(bol){
			if(name.indexOf("\\")>=0){
				a=name.substring(0,name.indexOf("\\"));
				name=name.substring(name.indexOf("\\")+1);
			}else{
				n=name;
				bol=false;
			}}
			if(bol){
			if(name.indexOf("\\")>=0){
				b=name.substring(0,name.indexOf("\\"));
				name=name.substring(name.indexOf("\\")+1);
			}else{
				n=a+"\\\\"+name;
				bol=false;
			}}
			if(bol){
			if(name.indexOf("\\")>=0){
				c=name.substring(0,name.indexOf("\\"));
				name=name.substring(name.indexOf("\\")+1);
			}else{
				n=a+"\\\\"+b+"\\\\"+name;
				bol=false;
			}}
			if(bol){
			if(name.indexOf("\\")>=0){
				d=name.substring(0,name.indexOf("\\"));
				name=name.substring(name.indexOf("\\")+1);
			}else{
				n=a+"\\\\"+b+"\\\\"+c+"\\\\"+name;
				bol=false;
			}}
			if(bol){
			if(name.indexOf("\\")>=0){
				e=name.substring(0,name.indexOf("\\"));
				name=name.substring(name.indexOf("\\")+1);
			}else{
				n=a+"\\\\"+b+"\\\\"+c+"\\\\"+d+"\\\\"+name;
				bol=false;
			}}
			if(bol){
			if(name.indexOf("\\")>=0){
				f=name.substring(0,name.indexOf("\\"));
				name=name.substring(name.indexOf("\\")+1);
			}else{
				n=a+"\\\\"+b+"\\\\"+c+"\\\\"+d+"\\\\"+e+"\\\\"+name;
				bol=false;
			}}
			if(bol){
			if(name.indexOf("\\")>=0){
				g=name.substring(0,name.indexOf("\\"));
				name=name.substring(name.indexOf("\\")+1);
			}else{
				n=a+"\\\\"+b+"\\\\"+c+"\\\\"+d+"\\\\"+e+"\\\\"+f+"\\\\"+name;
			}}

			receivingNotes.setMaterialName(n);
		}
		Page<ReceivingNotes> page = receivingNotesService.findPage(new Page<ReceivingNotes>(request, response), receivingNotes);
		return getBootstrapData(page);
	}

	/**
	 * 查看，增加，编辑收货单表单页面
	 */
	@RequiresPermissions(value={"wms:receivingNotes:view","wms:receivingNotes:add","wms:receivingNotes:edit"},logical=Logical.OR)
	@RequestMapping(value = "form")
	public String form(ReceivingNotes receivingNotes, Model model) {
		model.addAttribute("receivingNotes", receivingNotes);
		return "modules/wms/receivingNotesForm";
	}

	/**
	 * 保存收货单
	 */
	@ResponseBody
	@RequiresPermissions(value={"wms:receivingNotes:add","wms:receivingNotes:edit"},logical=Logical.OR)
	@RequestMapping(value = "save")
	public AjaxJson save(ReceivingNotes receivingNotes, Model model, RedirectAttributes redirectAttributes) throws Exception{
		AjaxJson j = new AjaxJson();
		if (!beanValidator(model, receivingNotes)){
			j.setSuccess(false);
			j.setMsg("非法参数！");
			return j;
		}
		receivingNotesService.save(receivingNotes);//新建或者编辑保存
		j.setSuccess(true);
		j.setMsg("保存收货单成功");
		return j;
	}
	
	/**
	 * 删除收货单
	 */
	@ResponseBody
	@RequiresPermissions("wms:receivingNotes:del")
	@RequestMapping(value = "delete")
	public AjaxJson delete(ReceivingNotes receivingNotes, RedirectAttributes redirectAttributes) {
		AjaxJson j = new AjaxJson();
		receivingNotesService.delete(receivingNotes);
		j.setMsg("删除收货单成功");
		return j;
	}
	@ResponseBody
	@RequestMapping("findListByPid")
	public String findListByPid(String pid){
		ReceivingNotes r=new ReceivingNotes();
		r.setPid(pid);
		List<ReceivingNotes> list=receivingNotesService.findList(r);
		Gson g=new Gson();
		return g.toJson(list).toString();
	}
	/**
	 * 批量删除收货单
	 */
	@ResponseBody
	@RequiresPermissions("wms:receivingNotes:del")
	@RequestMapping(value = "deleteAll")
	public AjaxJson deleteAll(String ids, RedirectAttributes redirectAttributes) {
		AjaxJson j = new AjaxJson();
		String idArray[] =ids.split(",");
		for(String id : idArray){
			receivingNotesService.delete(receivingNotesService.get(id));
		}
		j.setMsg("删除收货单成功");
		return j;
	}
	
	/**
	 * 导出excel文件
	 */
	@ResponseBody
	@RequiresPermissions("wms:receivingNotes:export")
    @RequestMapping(value = "export", method=RequestMethod.POST)
    public AjaxJson exportFile(ReceivingNotes receivingNotes, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		AjaxJson j = new AjaxJson();
		try {
            String fileName = "收货单"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
            Page<ReceivingNotes> page = receivingNotesService.findPage(new Page<ReceivingNotes>(request, response, -1), receivingNotes);
    		new ExportExcel("收货单", ReceivingNotes.class).setDataList(page.getList()).write(response, fileName).dispose();
    		j.setSuccess(true);
    		j.setMsg("导出成功！");
    		return j;
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg("导出收货单记录失败！失败信息："+e.getMessage());
		}
			return j;
    }

	/**
	 * 导入Excel数据

	 */
	@RequiresPermissions("wms:receivingNotes:import")
    @RequestMapping(value = "import", method=RequestMethod.POST)
    public String importFile(MultipartFile file, RedirectAttributes redirectAttributes) {
		try {
			int successNum = 0;
			int failureNum = 0;
			StringBuilder failureMsg = new StringBuilder();
			ImportExcel ei = new ImportExcel(file, 1, 0);
			List<ReceivingNotes> list = ei.getDataList(ReceivingNotes.class);
			for (ReceivingNotes receivingNotes : list){
				try{
					receivingNotesService.save(receivingNotes);
					successNum++;
				}catch(ConstraintViolationException ex){
					failureNum++;
				}catch (Exception ex) {
					failureNum++;
				}
			}
			if (failureNum>0){
				failureMsg.insert(0, "，失败 "+failureNum+" 条收货单记录。");
			}
			addMessage(redirectAttributes, "已成功导入 "+successNum+" 条收货单记录"+failureMsg);
		} catch (Exception e) {
			addMessage(redirectAttributes, "导入收货单失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/wms/receivingNotes/?repage";
    }
	
	/**
	 * 下载导入收货单数据模板
	 */
	@RequiresPermissions("wms:receivingNotes:import")
    @RequestMapping(value = "import/template")
    public String importFileTemplate(HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
            String fileName = "收货单数据导入模板.xlsx";
    		List<ReceivingNotes> list = Lists.newArrayList(); 
    		new ExportExcel("收货单数据", ReceivingNotes.class, 1).setDataList(list).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导入模板下载失败！失败信息："+e.getMessage());
		}
		return "redirect:"+Global.getAdminPath()+"/wms/receivingNotes/?repage";
    }

}